package com.imessage.action.manage.user;

import com.bean.ManageRole;
import com.bean.ManageRoleUser;
import com.bean.ManageUser;
import com.imessage.action.BaseAction;
import com.imessage.type.BaseState;
import com.imessage.utils.MD5Encrypt;
import com.imessage.vo.UserVo;
import com.imessage.web.Page;
import io.ebean.Expr;
import io.ebean.Expression;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

public class UserBaseAction extends BaseAction {

    protected final String datagrid="datagrid-manageUser-filter";

    protected UserBean getUserBean(ManageUser manageUser){
        UserBean userBean=new UserBean();
        userBean.setManageUser(manageUser);
        List<ManageRole> manageRoleList=roleMenuService.findManageRoleList(manageUser.getId());
        StringBuffer sb=new StringBuffer();
        for(ManageRole manageRole:manageRoleList){
            sb.append(manageRole.getName());
            sb.append(",");
        }
        userBean.setManageRoles(sb.toString());
        userBean.setId(manageUser.getId().toString());
        return userBean;
    }


    protected Page<UserBean> getUserBeanPage(Page<ManageUser> page){
        Page<UserBean> userBeanPage=new Page<UserBean>();
        userBeanPage.setTotalRow(page.getTotalRow());
        userBeanPage.setTotal(page.getTotal());
        userBeanPage.setPageCurrent(page.getPageCurrent());
        userBeanPage.setExtra(page.getExtra());
        userBeanPage.setPageSize(page.getPageSize());
        userBeanPage.setTotalPage(page.getTotalPage());
        userBeanPage.setList(new ArrayList<UserBean>());
        if(page.getList()==null||page.getList().size()==0){
            return userBeanPage;
        }
        List<UserBean> userBeanList=new ArrayList<UserBean>();
        for(ManageUser manageUser:page.getList()){
            UserBean userBean=new UserBean();
            userBean.setManageUser(manageUser);
            List<ManageRole> manageRoleList=roleMenuService.findManageRoleList(manageUser.getId());
            StringBuffer sb=new StringBuffer();
            for(ManageRole manageRole:manageRoleList){
                sb.append(manageRole.getName());
                sb.append(",");
            }
            userBean.setManageRoles(sb.toString());
            userBean.setId(manageUser.getId().toString());
            userBeanList.add(userBean);
        }
        userBeanPage.setList(userBeanList);
        return userBeanPage;
    }


    protected void setExpressionList(List<Expression> expressionList,UserVo userVo){
        if(StringUtils.isNotBlank(userVo.getName())){
            expressionList.add(Expr.contains("name", userVo.getName()));
        }
        if(StringUtils.isNotBlank(userVo.getState())){
            expressionList.add(Expr.eq("state", userVo.getState()));
        }else{
            expressionList.add(Expr.ne("state", BaseState.delete.getCode()));
        }
    }

    protected ManageRoleUser getManageUserRole(Long manage_user_id){
        List<Expression> expressionList=new ArrayList<Expression>();
        expressionList.add(Expr.eq("manageUserId", manage_user_id));
        List<ManageRoleUser> manageUserRoleList= baseService.findList( ManageRoleUser.class, expressionList);
        if(manageUserRoleList!=null && !manageUserRoleList.isEmpty() ){
            return manageUserRoleList.get(0);
        }
        return new ManageRoleUser();
    }

    protected List<ManageRoleUser> getManageUserRoleList(Long manage_user_id){
        List<Expression> expressionList=new ArrayList<Expression>();
        expressionList.add(Expr.eq("manageUserId", manage_user_id));
        return  baseService.findList(ManageRoleUser.class, expressionList);
    }

    protected List<ManageRole> getAllManageRole(){
        List<Expression> expressionList=new ArrayList<Expression>();
        expressionList.add(Expr.eq("state",  BaseState.normal.getCode()));
        return baseService.findList(ManageRole.class, expressionList);
    }

    protected boolean existManageUser(String username,String manage_user_id){
        List<Expression> expressionList=new ArrayList<Expression>();
        if(StringUtils.isNotBlank(username)){
            expressionList.add(Expr.eq("name", username));
        }
        if(StringUtils.isNotBlank(manage_user_id)){
            expressionList.add(Expr.ne("id", manage_user_id));
        }
        ManageUser manageUser =baseService.findObject(ManageUser.class, expressionList);
        if(manageUser==null){
            return false;
        }
        return true;
    }

    protected void saveManageUserRole(String[] roleid,Long manage_user_id){
        if(roleid==null || roleid.length==0){
            return ;
        }
        for(String id:roleid){
            ManageRole manageRole=baseService.findById(ManageRole.class,id);
            ManageRoleUser manageUserRole=new ManageRoleUser();
            manageUserRole.setManageRoleId(manageRole.getId());
            manageUserRole.setManageUserId(manage_user_id);
            baseService.saveObject(manageUserRole);
        }
    }

    protected void setModelAndView(ModelAndView modelAndView, ManageUser manageUser, ManageRoleUser manageUserRole){
        modelAndView.addObject("manageUserForm", manageUser);
        modelAndView.addObject("manageUserRoleForm",manageUserRole);
        modelAndView.addObject("manageRoleList", getAllManageRole());
        modelAndView.addObject("statusList", BaseState.values());
    }


    protected void setManageUser(UserVo userVo, ManageUser manageUser){
        copyProperties(userVo, manageUser);
        if(StringUtils.isNotBlank(userVo.getState())){
            manageUser.setState(BaseState.fromValue(userVo.getState()));
        }
        if(StringUtils.isNotBlank(userVo.getPasswd())){
            manageUser.setPasswd(MD5Encrypt.MD5(userVo.getPasswd()));
        }
    }
}
